package renthouse.view.information.landlordmanger.houseManger;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import renthouse.dao.HouseDao;
import renthouse.entry.House;
import renthouse.util.StringUtil;
import renthouse.view.sever.LoginFrame;
///*
// * Created by JFormDesigner on Fri May 21 18:52:54 CST 2021
// */
//


/**
 * @author Brainrain
 */
public class AddHouse extends JFrame {
    public static void main(String[] args) {
        AddHouse addHouse=new AddHouse();
        addHouse.setVisible(true);
    }
    public AddHouse() {
        initComponents();
    }



    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String addressVal = address.getText();
        String stateVal = (String) state.getSelectedItem();
        String styleVal = style.getText();
        String rentpriceVal = rentprice.getText();
        int masteridVal= LoginFrame.loginlandlord.getId();


        if (StringUtil.isNull(addressVal)) {
            JOptionPane.showMessageDialog(null, "地址不能为空");
            return;
        }

        if (StringUtil.isNull(styleVal)) {
            JOptionPane.showMessageDialog(null, "风格不能为空");
            return;
        }
        if (StringUtil.isNull(rentpriceVal)) {
            JOptionPane.showMessageDialog(null, "租金不能为空");
            return;
        }


        //todo 正则的判断    账号   密码   邮箱  手机号

        HouseDao houseDao = new HouseDao();
        House house = new House();

/*将房屋值信息传递表*/
        house.setMasterid(masteridVal);
        house.setAddress(addressVal);
        house.setState(stateVal);
        house.setStyle(styleVal);
        house.setRent(Integer.valueOf(rentpriceVal));

        boolean f = houseDao.insert(house);

        if (f) {
            JOptionPane.showMessageDialog(null, "添加成功");
        } else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }

        setVisible(false);
        HouseInFo houseInFo=new HouseInFo();
        houseInFo.setVisible(true);
    }

    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label2 = new JLabel();
        address = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        style = new JTextField();
        label5 = new JLabel();
        rentprice = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        state = new JComboBox<>();

        //======== this ========
        setTitle("\u6dfb\u52a0\u623f\u5c4b");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label2 ----
            label2.setText("\u5730\u5740");
            panel1.add(label2);
            label2.setBounds(55, 30, 50, 17);
            panel1.add(address);
            address.setBounds(110, 25, 170, 25);

            //---- label3 ----
            label3.setText("\u72b6\u6001");
            panel1.add(label3);
            label3.setBounds(335, 30, 50, 17);

            //---- label4 ----
            label4.setText("\u98ce\u683c");
            panel1.add(label4);
            label4.setBounds(55, 90, 50, 17);
            panel1.add(style);
            style.setBounds(110, 85, 170, 25);

            //---- label5 ----
            label5.setText("\u6bcf\u6708\u79df\u91d1");
            panel1.add(label5);
            label5.setBounds(330, 90, 50, 17);
            panel1.add(rentprice);
            rentprice.setBounds(400, 85, 170, 25);

            //---- button1 ----
            button1.setText("\u786e\u5b9a");
            button1.addActionListener(e -> button1ActionPerformed(e));
            panel1.add(button1);
            button1.setBounds(new Rectangle(new Point(170, 240), button1.getPreferredSize()));

            //---- button2 ----
            button2.setText("\u53d6\u6d88");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);
            button2.setBounds(420, 240, 60, 25);

            //---- state ----
            state.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u5df2\u51fa\u79df",
                "\u5f85\u51fa\u79df"
            }));
            panel1.add(state);
            state.setBounds(400, 30, 165, state.getPreferredSize().height);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 685, 425);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label2;
    private JTextField address;
    private JLabel label3;
    private JLabel label4;
    private JTextField style;
    private JLabel label5;
    private JTextField rentprice;
    private JButton button1;
    private JButton button2;
    private JComboBox<String> state;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}


